package com.njwb.day07.Stus;

public class StudentServiceImpl implements StudentService {

    private static final StudentDao studentDao = new StudentDaoImpl();

    @Override
    public void add() {
        studentDao.add();

    }

    @Override
    public String showNo(int no) {
        Student stu = studentDao.SelectNo(no);
        if (stu == null) {
            return "学号不存在！";
        }
        return "学号\t姓名\t成绩\n" + stu.getNo() + "\t" + stu.getName() + "\t" + stu.getScores();
    }


    @Override
    public String deleteStudent(int no) {
        Student existing = studentDao.SelectNo(no);
        if (existing == null) {
            return "学号不存在，无法删除";
        }
        studentDao.deleteStudent(no);
        return "删除成功！";
    }

    @Override
    public String addStudent(int no, String name, double scores) {
        Student existing = studentDao.SelectNo(no);
        if (existing != null) {
            return "学号已存在，无法添加！";
        }
        studentDao.addStudent(no, name, scores);
        return "添加成功！";
    }


    @Override
    public String findAll() {
        Student[] stus = studentDao.findAll();
        if (stus == null || stus.length == 0) {
            return "当前没有学生信息！";
        }

        StringBuilder sb = new StringBuilder("学号\t姓名\t成绩\n");
        for (Student s : stus) {
            sb.append(s.getNo()).append("\t")
                    .append(s.getName()).append("\t")
                    .append(s.getScores()).append("\n");
        }
        return sb.toString();
    }


    @Override
    public Student alterStudent(int no) {

        return studentDao.SelectNo(no);


    }

    @Override
    public String alterStudent(int no, String name, double score) {
        studentDao.alterStudent(no, name, score);
        return "修改成功！！！";

    }


    @Override
    public String showName(String name) {
        Student student = studentDao.showName(name);
        if (student == null) {
            return "学生不存在！！！";
        }
        return "学号\t姓名\t成绩\n" + student.getNo() + "\t" + student.getName() + "\t" + student.getScores();
    }


    @Override
    public String showDesc() {
        Student[] stus = studentDao.showDesc();
        if (stus == null || stus.length == 0) {
            return "当前没有学生信息！";
        }

        StringBuilder sb = new StringBuilder("学号\t姓名\t成绩\n");
        for (Student s : stus) {
            sb.append(s.getNo()).append("\t")
                    .append(s.getName()).append("\t")
                    .append(s.getScores()).append("\n");
        }
        return sb.toString();
    }


}
